﻿using DHJ.Cost.EntityFrameworkCore;
using DHJ.Cost.ManHours;
using DHJ.Cost.NumToString;
using DHJ.Cost.Tasks;
using DHJ.Cost.Timing;
using DHJ.Cost.Workshops;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using Abp.Timing;

namespace DHJ.Cost.Migrations.Seed.Host
{
    public class DefaultManHoursCreator
    {
        private readonly CostDbContext _context;

        public DefaultManHoursCreator(CostDbContext context)
        {
            _context = context;
        }

        public void Create()
        {
            CreateManHours();
        }

        private void CreateManHours()
        {
            const string thisMonth = "201904";

            // 任务关系
            /*
             *     T1(B1) |-- T2(B2)
             *            |-- T3(B3)
             *            |-- T4(B4) |-- T5(B5.51)
             *                       |-- T6(B6/B7.B71)
             *          
             *          
             */
            var task1 = new Task(null, "T1", "CZ-5", "P1", "p1", 1, 1);
            var task2 = new Task(null, "T2", "CZ-5", "P2", "p2", 3, 3);
            var task3 = new Task(null, "T3", "CZ-5", "P3", "p3", 3, 3);
            var task4 = new Task(null, "T4", "CZ-5", "P4", "p4", 1, 1);
            var task5 = new Task(null, "T5", "CZ-5", "P5", "p5", 5, 5);
            var task6 = new Task(null, "T6", "CZ-5", "P6", "p6", 6, 6);
            CreateTaskIfNotExists(task1);
            CreateTaskIfNotExists(task2);
            CreateTaskIfNotExists(task3);
            CreateTaskIfNotExists(task4);
            CreateTaskIfNotExists(task5);
            CreateTaskIfNotExists(task6);


            var batch1 = new Batch("Batch" + thisMonth + NumToStringHelper.NumToString(1, 4), task1.Id, 1, "QC1");
            var batch2 = new Batch("Batch" + thisMonth + NumToStringHelper.NumToString(2, 4), task2.Id, 3, "QC2");
            var batch3 = new Batch("Batch" + thisMonth + NumToStringHelper.NumToString(3, 4), task3.Id, 3, "QC3");
            var batch4 = new Batch("Batch" + thisMonth + NumToStringHelper.NumToString(4, 4), task4.Id, 1, "QC4");
            var batch5 = new Batch("Batch" + thisMonth + NumToStringHelper.NumToString(5, 4), task5.Id, 3, "QC5");
            var batch51 = new Batch("Batch" + thisMonth + NumToStringHelper.NumToString(5, 4) + "-001", task5.Id, 2, "QC51", batch5.Id, 5);
            var batch6 = new Batch("Batch" + thisMonth + NumToStringHelper.NumToString(6, 4), task6.Id, 2, "QC6");
            var batch7 = new Batch("Batch" + thisMonth + NumToStringHelper.NumToString(7, 4), task6.Id, 2, "QC7");
            var batch71 = new Batch("Batch" + thisMonth + NumToStringHelper.NumToString(7, 4) + "-001", task6.Id, 2, "QC71", batch7.Id, 4);
            CreateBatchIfNotExists(batch1);
            CreateBatchIfNotExists(batch2);
            CreateBatchIfNotExists(batch3);
            CreateBatchIfNotExists(batch4);
            CreateBatchIfNotExists(batch5);
            CreateBatchIfNotExists(batch51);
            CreateBatchIfNotExists(batch6);
            CreateBatchIfNotExists(batch7);
            CreateBatchIfNotExists(batch71);

            var mh1 = new BatchManHour(new OccurrenceTime(Clock.Now.Year, 4), Workshop.Machine, batch1.Id, 1, new ManHour(100));
            var mh2 = new BatchManHour(new OccurrenceTime(Clock.Now.Year, 4), Workshop.Machine, batch2.Id, 3, new ManHour(50));
            var mh3 = new BatchManHour(new OccurrenceTime(Clock.Now.Year, 4), Workshop.Machine, batch3.Id, 3, new ManHour(20));
            var mh4 = new BatchManHour(new OccurrenceTime(Clock.Now.Year, 4), Workshop.Machine, batch4.Id, 1, new ManHour(10));
            var mh51 = new BatchManHour(new OccurrenceTime(Clock.Now.Year, 4), Workshop.Machine, batch51.Id, 2, new ManHour(8));
            var mh6 = new BatchManHour(new OccurrenceTime(Clock.Now.Year, 4), Workshop.Machine, batch6.Id, 2, new ManHour(6));
            var mh71 = new BatchManHour(new OccurrenceTime(Clock.Now.Year, 4), Workshop.Machine, batch71.Id, 2, new ManHour(7));
            CreateManHourIfNotExists(mh1);
            CreateManHourIfNotExists(mh2);
            CreateManHourIfNotExists(mh3);
            CreateManHourIfNotExists(mh4);
            CreateManHourIfNotExists(mh51);
            CreateManHourIfNotExists(mh6);
            CreateManHourIfNotExists(mh71);


            // 添加装配关系
            /*
             *     00001 |-- 00002
             *           |-- 00003
             *           |-- 00004--|-- 00005-001
             *                      |-- 00007-001
             *          
             *          
             */

            var assem2 = new AssemRel()
            {
                AssemId = batch1.Id,
                PartId = batch2.Id,
                PartQuantity = 2
            };
            CreateAssemIfNotExists(assem2);

            var assem3 = new AssemRel()
            {
                AssemId = batch1.Id,
                PartId = batch3.Id,
                PartQuantity = 3
            };
            CreateAssemIfNotExists(assem3);

            var assem4 = new AssemRel()
            {
                AssemId = batch1.Id,
                PartId = batch4.Id,
                PartQuantity = 1
            };
            CreateAssemIfNotExists(assem4);

            var assem5 = new AssemRel()
            {
                AssemId = batch4.Id,
                PartId = batch51.Id,
                PartQuantity = 1
            };
            CreateAssemIfNotExists(assem5);

            var assem6 = new AssemRel()
            {
                AssemId = batch4.Id,
                PartId = batch71.Id,
                PartQuantity = 2
            };
            CreateAssemIfNotExists(assem6);
        }

        private void CreateTaskIfNotExists(Task task)
        {
            var queryTask = _context.Tasks.IgnoreQueryFilters()
                .FirstOrDefault(ef => ef.TaskNum == task.TaskNum);

            if (queryTask == null)
            {
                _context.Tasks.Add(task);
            }
        }

        private void CreateBatchIfNotExists(Batch batch)
        {
            var queryBatch = _context.Batches.IgnoreQueryFilters()
                .FirstOrDefault(ef => ef.Id == batch.Id);

            if (queryBatch == null)
            {
                _context.Batches.Add(batch);
            }
        }

        private void CreateManHourIfNotExists(BatchManHour manHour)
        {
            var queryManHour = _context.BatchManHours.IgnoreQueryFilters()
                .FirstOrDefault(ef => ef.BatchId == manHour.BatchId);

            if (queryManHour == null)
            {
                _context.BatchManHours.Add(manHour);
            }
        }

        private void CreateAssemIfNotExists(AssemRel assem)
        {
            var queryAssem = _context.AssemRels.IgnoreQueryFilters()
                .FirstOrDefault(ef => ef.AssemId == assem.AssemId && ef.PartId == assem.PartId);

            if (queryAssem == null)
            {
                _context.AssemRels.Add(assem);
            }
        }
    }
}